<?php
   class  ApiCenter_SalesApi_Model_Api2_Orderapi  extends  Mage_Api2_Model_Resource {
      
       
       const PARAM_GIFT_MESSAGE   = '_gift_message';
       const PARAM_ORDER_COMMENTS = '_order_comments';
       const PARAM_PAYMENT_METHOD = '_payment_method';
       const PARAM_TAX_NAME       = '_tax_name';
       const PARAM_TAX_RATE       = '_tax_rate';
       const  VENDOR_NO_EXIST     ="商家信息有误";
       const  DEFAULT_WIDTH=        "100";
       const  DEFAULT_HEIGHT=        "100";
       const  NOIMAGE ="";
       const  NOTRACKING="no have ordertracking";
       const  IS_CONFIRM =2;
        
       const  NO_CONFIRM =1;
       protected  function _loadVendor($vendorId){
           $vendor = Mage::getModel("vendors/vendor")->load($vendorId);
           if(!$vendor->getId()){
               $this->_critical(self::VENDOR_NO_EXIST,400);
           }
           return $vendor;
       }
       
        
       /**
        * Retrieve collection instance for single order
        *
        */
       protected function _getCollectionForSingleRetrieve($orderId,$vendorId)
       {
           /** @var $collection Mage_Sales_Model_Resource_Order_Collection */
           $collection = Mage::getResourceModel('salesapi/order_collection');
           $collection->addFieldToFilter("main_table.vendor_id", $vendorId);
           $collection->addFieldToFilter($collection->getResource()->getIdFieldName(), $orderId);
           $collection->getSelect()
            ->joinleft(array('d' => 'tb_logistics_product'),
                   'd.proCode = main_table.v_carrier_product',
                   array(
                       "shipping_method"=>"d.proName",
                   ));
           return $collection;
          
       }
       

    /**
     * Retrieve a list or orders' addresses in a form of [order ID => array of addresses, ...]
     *
     * @param array $orderIds Orders identifiers
     * @return array
     */
    protected function _getAddresses(array $orderIds)
    {
        $addresses = array();
    
        if ($this->_isSubCallAllowed('order_address')) {
            /** @var $addressesFilter Mage_Api2_Model_Acl_Filter */
            $addressesFilter = $this->_getSubModel('order_address', array())->getFilter();
            // do addresses request if at least one attribute allowed
            if ($addressesFilter->getAllowedAttributes()) {
                /* @var $collection Mage_Sales_Model_Resource_Order_Address_Collection */
                $collection = Mage::getResourceModel('salesapi/order_address_collection');
    
                $collection->addFieldToFilter('parent_id', $orderIds);
    
                foreach ($collection->getItems() as $item) {
                    $addresses[$item->getParentId()][] = $addressesFilter->out($item->toArray());
                }
            }
        }
        return $addresses;
    }
    /**
     * Retrieve a list or orders' items in a form of [order ID => array of items, ...]
     *
     * @param array $orderIds Orders identifiers
     * @return array
     */
    protected function _getItems(array $orderIds)
    {
        $items = array();
        $helper = Mage::helper("salesapi");
        if ($this->_isSubCallAllowed('orderapi_orderwarehouse')) {
            /** @var $itemsFilter Mage_Api2_Model_Acl_Filter */
            $itemsFilter = $this->_getSubModel('orderapi_orderwarehouse', array())->getFilter();
            // do items request if at least one attribute allowed
            if ($itemsFilter->getAllowedAttributes()) {
                
                /* @var $collection Mage_Sales_Model_Resource_Order_Item_Collection */
                $collection = Mage::getResourceModel('salesapi/order_item_collection');
    
                $collection->addFieldToFilter('order_id', $orderIds);
                $collection->getSelect()
             
                ->join(array('d' => 'catalog_product_entity'),
                    'd.sku = main_table.sku',
                    array(
                        "product_id"=>"d.entity_id",
                    ))
                ->joinLeft(array("k"=>"catalog_product_super_link"),
                    "k.product_id = d.entity_id",
                    array("parent_id"=>"k.parent_id")
                );
 
                $i=0;
                foreach ($collection->getItems() as $item) {
                   $items[$i] = $itemsFilter->out($item->toArray());
                   $items[$i]['img_url'] =$helper->_loadImgUrl($item->getSku());
                   $i++;
                }
            }
        }
        return $items;
    }

    protected  function getOdertrackingInfo($orderid){
        $trackingInfo= '';
            try {
                $trackingInfo = Mage::helper('ordertrackinginfo')->getTrackingInfo($orderid);
            } catch (Exception $e) {
                Mage::log("获取物流接口信息错误",1,"api.log");
            }
            return  $trackingInfo?json_decode(json_encode($trackingInfo),true):"";   
    }
 }